package broadcast;

import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;

import common.Message;

public class MessageLog {
	private static final int MAX_MSG_WINDOW_SIZE = 50;
	
	private Set<Message> messageSet;
	private Queue<Message> messageQueue;
	
	public MessageLog() {
		messageSet = new HashSet<Message>();
		messageQueue = new ArrayDeque<Message>();
	}
	
	public boolean contains(Message m) {
		return messageSet.contains(m);
	}
	
	public void addMessage(Message m) {
		if(contains(m)) {
			return;
		}
		if(messageSet.size() >= MAX_MSG_WINDOW_SIZE) {
			Message victim = messageQueue.remove();
			messageSet.remove(victim);
			messageQueue.add(m);
			messageSet.add(m);
		} else {
			messageSet.add(m);
			messageQueue.add(m);
		}
	}
	public void reset() {
		messageSet = new HashSet<Message>();
		messageQueue = new ArrayDeque<Message>();
	}
}
